package com.weiso.weisophonebook.thread;

import java.util.ArrayList;
import java.util.List;

import com.weiso.weisophonebook.Constants;
import com.weiso.weisophonebook.HCData;
import com.weiso.weisophonebook.bean.LinkMan;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MyAsyncQueryHandler extends AsyncQueryHandler {
	Handler handler;
	List<LinkMan> linkmanlist;
	public MyAsyncQueryHandler(ContentResolver cr,Handler handler) {
		super(cr);
		this.handler = handler;
	}

	protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
//		  Log.e("onQueryComplete", "aaa"+cursor.getCount());
		  if (cursor != null && cursor.getCount() > 0) {  
			  linkmanlist = new  ArrayList<LinkMan>();
			  cursor.moveToFirst();
		  }else{
			  handler.sendEmptyMessage(Constants.QueryERR);
			  return;
		  }

		  LinkMan linkman;
		  String str1 = "";
		  String str2 = "";
		  HCData.maxitemcount=cursor.getCount();
		  for (int i = 0; i < cursor.getCount(); i++) {
			  linkman = new LinkMan();
			  cursor.moveToPosition(i);
			  String name = cursor.getString(1);  
              String number = cursor.getString(2);  
              String sortkey = cursor.getString(3);
              String photoid = cursor.getString(4);
              String lookup = cursor.getString(5);
              
              linkman.setName(name);
              linkman.setNumber(number);
              linkman.setSortkey(sortkey);
              str1 = linkman.getHeadchar();
              HCData.placeint.put(i, str1);
              if(!str1.equals(str2)){
            	  str2 = str1;
            	  HCData.placestr.put(str2, i);
            	  linkman.setAlphashow(true); 
              }
              linkman.setHeadimg(photoid);
              linkman.setLookup(lookup);
              linkman.setWeizhi(i);
              linkmanlist.add(linkman);
		  }
		  Log.e("onQueryComplete", "aaa3:"+linkmanlist.size());
		  if(linkmanlist.size()>0){
			  Message msg = new Message();
			  msg.what = Constants.QuerySUCCESS1;
			  msg.obj = linkmanlist;
			  handler.sendMessage(msg);
		  }else if(linkmanlist.size()==0){
			  handler.sendEmptyMessage(Constants.QuerySUCCESS2);
		  }
		  Log.e("onQueryComplete", "aaa4"+linkmanlist.size());
		
	}
	
	

}
